package com.hps.shiro.auth;

import com.hps.shiro.model.User;
import com.hps.shiro.service.UserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author heps
 * @since 2020/6/8 17:40
 */
public class UserRealm extends AuthorizingRealm {

  @Autowired
  private UserService userService;

  @Override
  protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)
      throws AuthenticationException {
    String principal = (String) authenticationToken.getPrincipal();
    if (principal == null) {
      return null;
    }
    User user = userService.findByUserName(principal);
    if (user == null) {
      return null;
    }
    return new SimpleAuthenticationInfo(user, user.getPassword(), getName());
  }

  @Override
  protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    return null;
  }
}
